package com.gg.fmallspringboot.controller;

import com.gg.fmallspringboot.entity.Orders;
import com.gg.fmallspringboot.model.Result;
import com.gg.fmallspringboot.service.impl.OrderServiceImpl;
import com.gg.fmallspringboot.service.impl.WCPayServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 作者:Miki
 * 时间:2021/6/20 22:56
 */
@RestController
@RequestMapping("/wx-pay")
public class WCPayController {

    @Autowired
    private WCPayServiceImpl wcPayService;
    @Autowired
    private OrderServiceImpl orderService;

    @GetMapping("/orderWXPay")
    public Result wxPay(String orderId){
        //根据订单id在数据库中查询需要支付的金额，订单的状态(是否已支付)
        Orders order = orderService.getOrderById(orderId);
        Result result;
        if (order!=null&&Integer.parseInt(order.getStatus())==1){
            String url = (String)wcPayService.unifiedorder(orderId, 1, "http://miki.free.idcfengye.com/order/update").getData();
            result = new Result(0,"请支付",url);
        }else{
            result =new Result(1,"订单已支付或订单不存在");
        }
        System.out.println(result);
        return result;
    }
}
